package leetcode;

import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 7/25/2022 8:26 AM
 */
public class Test12 {

    private static final Logger logger = LoggerFactory.getLogger(Test12.class);

    /**
     * 整数转罗马数字
     */
    @Test
    public void test() {
        int num = 3;
        String s = intToRoman(58);
        logger.info(s);
    }

    public String intToRoman(int num) {
        int tempNum = num;
        Map<Integer, String> map = new LinkedHashMap<>(16);
        map.put(1000, "M");
        map.put(900, "CM");
        map.put(500, "D");
        map.put(400, "CD");
        map.put(100, "C");
        map.put(90, "XC");
        map.put(50, "L");
        map.put(40, "XL");
        map.put(10, "X");
        map.put(9, "IX");
        map.put(5, "V");
        map.put(4, "IV");
        map.put(1, "I");
        StringBuilder s = new StringBuilder();
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            Integer key = entry.getKey();
            while (tempNum >= key) {
                tempNum = tempNum - key;
                s.append(entry.getValue());
            }
            if (tempNum == 0) {
                break;
            }
        }
        return s.toString();
    }
}
